package com.stepforward.me;

import android.app.Activity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import java.net.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;


public class pghTwitter extends Activity {
    /** Called when the activity is first created. */
    
    /**
    public class twitterStatus {
		String status;
		String time;
		
		void setStatus(String s){ this.status = s;}
		void setTime(String t){ this.time = t;}
		
		String getStatus(){ return this.status;}
		String getTime(){ return this.time;}
	}
	
	ArrayList <twitterStatus> twitterStatuses;
	
	public String getTweets() throws Exception {

	  String url = "http://api.twitter.com/1/statuses/user_timeline.xml?id=pghtransit&count=20&page=1";
	  String inputLine="";
	  String line = "";
	
	  this.twitterStatuses = new ArrayList<twitterStatus>(); 
	   
	  System.out.println("" + url);
	  try {
	    URL yahoo = new URL(url);
		URLConnection yc = yahoo.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
			      
		while ((inputLine = in.readLine()) != null) {
			line += inputLine + "\n";
	    }	  	  
		in.close();
	    } catch (Exception e) {
		}
	    
	     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	      DocumentBuilder db = dbf.newDocumentBuilder();
	      Document doc = db.parse(new InputSource(new StringReader(line)));
	      doc.getDocumentElement().normalize();

	      NodeList nList = doc.getElementsByTagName("status");

	      
	      for (int i = 0; i < 20; i++) {
		    Node nNode = nList.item(i);
		    Element eElement = (Element) nNode;
		    twitterStatus t = new twitterStatus();
		    String temp [] = getTagValue("created_at",eElement).split("\\+");
		    
		    t.setTime(temp[0]);
		    t.setStatus(getTagValue("text",eElement));
	        twitterStatuses.add(t);
	      }
		return line;
		}

		public void printResults() {
			for (twitterStatus t: twitterStatuses) {
				System.out.println("Time: "+t.getTime());
				System.out.println("Status: "+t.getStatus());
			}
			
		}

	  private static String getTagValue(String sTag, Element eElement){
	    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	    Node nValue = (Node) nlList.item(0); 
	 
	    return nValue.getNodeValue();  
	    
	    
	   }
	  **/
	  private ListView wordsList;
	  ArrayList <String> t = new ArrayList<String>();

	  @Override
	    public void onCreate(Bundle savedInstanceState)  {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.twittermain);
	        /**
	        String b ="";
			try {
				 b=	getTweets();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	        ArrayList <String> tweets = new ArrayList<String>();
	       // tweets.add("test");
	        
	        for (twitterStatus t: this.twitterStatuses) {
	        	String tweet = t.getTime()+": "+t.getStatus();
				tweets.add(tweet);
			}
			 **/
			 t.add("Mon May 02 18:00:47 : Temp stop changes: 14, 18; closures near PNC Park for film shoot tomorrow, start of service thru 10p (Weds rain date): http://ow.ly/4Io1A");
			 t.add("Mon May 02 17:51:36 : @hanlam84 Generally, yes, a breakdown will cause a delay. To answer your other question, there are no more routes to change to O-something.");
			 t.add("Mon May 02 17:34:36 : @leogurl35 The east car is running, but it cannot accommodate wheelchairs so we are running that car plus bus shuttle.");
			 t.add("Mon May 02 17:16:14 : @hanlam84 Yes. Please keep in mind that due to staffing limitations & other work duties we may not respond immediately to all questions.");
			 t.add("Mon May 02 17:14:03 : Mon Incline's west car out of service. Since east car is not wheelchair accessible, we are running shuttle bus btwn upper & lower stations.");
			 t.add("Mon May 02 16:29:48 : @hanlam84 You should still put in the complaint. Time & location you boarded plus direction & mention it was an artic - that should be ok.");
			 t.add("Mon May 02 15:30:30 : Rider Rewards Deal of the Day: 10% off lunch & dinner and 20% off catering at T's Restaurant & Lounge, Swissvale, with transit pass.");
			 t.add("Mon May 02 15:25:00 : @BobbyOBobby If something like a P1 breaks down, traffic dept will make follower buses aware that those riders will be transferring.");
			 t.add("Mon May 02 15:22:29 : @BobbyOBobby In most cases there will be enough transfers. Systemwide, most riders use passes.");
			 t.add("Mon May 02 14:53:31 : @TransitGuru @hanlam84 The driver is supposed to give out transfers in that kind of situation, so I suggest putting in a complaint.");

	        wordsList = (ListView) findViewById(R.id.listView1);
	        wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,t));

	    }
}